<?php
/**
 * app 操作记录模型
 * Created by PhpStorm.
 * User: zhang-hong
 * Date: 2017/3/7
 * Time: 15:44
 */
class LogRecordModel extends BaseModel
{

    //添加操作
    public function add($biao_name,$insert_data)
    {
        $insert = DB::insert($biao_name)
            ->columns(array_keys($insert_data))
            ->values(array_values($insert_data));
        list($insert_id, $affected_rows) = $insert->execute();
        return $insert_id;
    }
    // 使用gzcompress对Json字符串进行解压缩
    /**
     * 使用gzcompress对Json字符串进行解压缩
     * @param  $str_json
     * @param  $compression_http_data
     * @return $return_value
     */
    public function uncompress($str_json,$compression_http_data)
    {
        if(empty($str_json))
        {
            return "";
        }

        $return_value = $str_json;
        if(!empty($compression_http_data) && $compression_http_data == "on")
        {
            // json字符串解析为数组
            $arr = json_decode($str_json);
            // 数组转化为字符串(经前台压缩过的)
            $str_compressed = $this->getStringFromBytes($arr);
            // 压缩次数
            $count = Yaconf::get("compress_count");
            if(empty($count)||$count<1)
            {
                $count = 1;
            }
            // 得到解压缩的字符串
            for($i = 0; $i < $count; $i++)
            {
                $str_uncompressed = gzuncompress($str_compressed);
            }

            // 空数据
            if($str_uncompressed == "[]")
            {
                $return_value = "";
            }
            else
            {
                $return_value = $str_uncompressed;
            }
        }
        return $return_value;
    }

    // 字节数组转化为字符串
    /**
     * 字节数组转化为字符串
     * @param  $bytes
     * @return $str
     */
    public function getStringFromBytes($bytes)
    {
        $str = '';
        foreach($bytes as $ch)
        {
            $str .= chr($ch);
        }
        return $str;
    }
}